package com.dmzapp.cashoffer.service;

import android.content.Context;

import com.dmzapp.cashoffer.component.AppCache;
import com.dmzapp.cashoffer.util.HttpUtil;
import com.dmzapp.cashoffer.util.TrapLogUtil;
import com.dmzapp.cashoffer.vo.AppInfo;
import com.dmzapp.cashoffer.vo.AppSearchResult;
import com.dmzapp.cashoffer.vo.UserAccount;

public class GetNotifyTask implements Runnable {

    private Context context;
    
    public GetNotifyTask(Context context) {
        this.context = context;
    }
    
    @Override
    public void run() {
        
        try {
            
            if (context == null) {
                TrapLogUtil.logE("context is null", new Exception());
            }
            
            UserAccount.getInstance().init(context);
            AppSearchResult result = HttpUtil.filterAppByuser(context, 0);
            AppCache.getInstance().setTotal(result.getTotal());
            AppCache.getInstance().updateCachelist(result.getResultList());

            int taskNumber = 0;
            
            for (AppInfo appinfo : result.getResultList()) {
                
                if (!appinfo.isIntalled()) {
                    taskNumber++;
                }
                
            }
            
            if (taskNumber > 0) {
                DMZNotificationManager.notifyNewTask(context, taskNumber);
            }
            
        } catch (Throwable e) {
            
            TrapLogUtil.logE("notify failed", e);
        }
    }

}
